from gm.api import *
import datetime
import numpy as np
import pandas as pd
from MSCI_tools import 根据市值对股票池因子中性化的写法 as factor
from MSCI_tools import 计算贝塔值和自定义的波动率_ as beta_and_vol
from MSCI_tools import msci_tools as tools
from MSCI_tools import 计算RSRS as rsrs_fun

set_token("a71a8083b68e73817e93f7f196b030482abe5939")
day_time,hour_and_mins=str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')).split(" ")

"""
排名条件设市盈率从小到大，权重2；  PETTM
股息率从大到小，权重1；
市净率从小到大，权重3；
历史贝塔从小到大，权重2；
自定义波动率指标从小到大权重10，
"""

symbol_list = tools.read_MSCI()

df = get_fundamentals(table='trading_derivative_indicator', symbols=symbol_list, start_date=day_time,
                              end_date=day_time, fields="PETTM", filter="PETTM > 0")
symbol_list = tools.get_data_value(df,"symbol")
_symbol_list = symbol_list
symbol_list = []
for _ in _symbol_list:
    symbol_list.append(_)

#symbol_list = ["SZSE.300296","SHSE.000001","SHSE.601801"]

ROEAVGCUT = factor.fast_batch_get_neutralized_factor(symbol_list,"ROEAVGCUT",day_time,more_is_better=False)
DY = factor.fast_batch_get_neutralized_factor(symbol_list,"DY",day_time,more_is_better=True)
PB = factor.fast_batch_get_neutralized_factor(symbol_list,"PB",day_time,more_is_better=True)
ROIC = factor.fast_batch_get_neutralized_factor(symbol_list,"PB",day_time,more_is_better=True)


beta = {}
volatility = {}
rsrs = {}

for symbol in symbol_list:
    beta[symbol] = beta_and_vol.get_beta_weight_2(symbol,day_time,count=60)
    volatility[symbol] = beta_and_vol.get_volatility_normal(symbol,day_time,count=60)
    rsrs[symbol] = rsrs_fun.get_rsrs_weight(symbol,day_time)




df = pd.DataFrame([])
df["symbol"] = ROEAVGCUT.keys()
df["ROEAVGCUT"] = ROEAVGCUT.values()
df["ROIC"] = ROIC.values()
df["PB"] = PB.values()
df["DY"] = DY.values()
df["beta"] = beta.values()
df["volatility"] = volatility.values()
df["rsrs"] = rsrs.values()


weight = [-1,-1,-1,-1,-1,-1,-1]

symbol_list = tools.from_df_get_symbol_list_by_score(df,weight,start=1)

print(symbol_list)